package com.jl.jlapp.mvp.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog.Builder;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.jl.jlapp.R;
import com.jl.jlapp.eneity.VersionMessageModel;
import com.jl.jlapp.mvp.base.MyApplication;
import com.jl.jlapp.mvp.fragment.ClassifityFragment;
import com.jl.jlapp.mvp.fragment.Fragment_FirstPages;
import com.jl.jlapp.mvp.fragment.Fragment_User;
import com.jl.jlapp.mvp.fragment.ShopCartFragment;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.service.DownloadService;
import com.jl.jlapp.utils.Tools;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BaseActivity
        extends FragmentActivity {
    private static final int DOWN_ERROR = 2;
    private static final int GET_UNDATAINFO_ERROR = 1;
    public static boolean IS_CHECK_NO_CHANGE = false;
    private static String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"};
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static final int UPDATA_CLIENT = 0;
    ArrayList<Fragment> fragmentList;
    FragmentManager fragmentManager;
    Fragment_FirstPages fragment_FirstPages;
    ClassifityFragment fragment_classify;
    ShopCartFragment fragment_shoppingCat;
    Fragment_User fragment_user;
    ArrayList<RadioButton> listRadioButton;
    RadioButton rb1;
    RadioButton rb2;
    RadioButton rb3;
    RadioButton rb4;
    RadioGroup rg;
    int shouldReplaceFragment = 0;
    FragmentTransaction transaction;
    int userId = 0;
    VersionMessageModel.ResultDataBean versionMessageBean = null;
    private String versionName = "";

    public static File getFileFromServer(String paramString, ProgressDialog paramProgressDialog)
            throws Exception {
        Log.d("aaaaaaaasetting", "getFileFromServer");
        if (Environment.getExternalStorageState().equals("mounted")) {
            URLConnection urlConnection = new URL(paramString).openConnection();
            urlConnection.setConnectTimeout(5000);
            paramProgressDialog.setMax(urlConnection.getContentLength());
            InputStream inputStream = urlConnection.getInputStream();
            File localFile = new File(Environment.getExternalStorageDirectory(), "jlfood.apk");
            FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
            BufferedInputStream localBufferedInputStream = new BufferedInputStream(inputStream);
//      byte[] arrayOfByte = new byte['Ѐ'];
//      int i = 0;
//      for (;;)
//      {
//        int j = localBufferedInputStream.read(arrayOfByte);
//        if (j == -1) {
//          break;
//        }
//        localFileOutputStream.write(arrayOfByte, 0, j);
//        i += j;
//        paramProgressDialog.setProgress(i);
//      }
//      localFileOutputStream.close();
//      localBufferedInputStream.close();
//      paramString.close();close
            return localFile;
        }
        return null;
    }

    private void getNewVersionMessage() {
        Net.get().getNewVersionMessage(0)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<VersionMessageModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(VersionMessageModel versionMessageModel) {

                    }
                });
    }

    private String getVersionName()
            throws Exception {
        PackageInfo localPackageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        Log.d("TAG", "版本号" + localPackageInfo.versionCode);
        Log.d("TAG", "版本名" + localPackageInfo.versionName);
        Editor localEditor = getSharedPreferences("JLAppData", 0).edit();
        localEditor.putInt("versionCode", localPackageInfo.versionCode);
        localEditor.putString("versionName", localPackageInfo.versionName);
        localEditor.commit();
        return localPackageInfo.versionName;
    }

    private void noMustChange() {
        Builder localBuilder = new Builder(this);
        localBuilder.setTitle("版本升级");
        localBuilder.setMessage("检测到最新版本，请及时更新！");
        Log.d("apkurlaaaaaaaaa", "apkurl:http://117.158.178.202:8001/jlapk/jlfood.apk");
        localBuilder.setPositiveButton("确定", new OnClickListener() {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                BaseActivity.IS_CHECK_NO_CHANGE = false;
                paramAnonymousDialogInterface.dismiss();
                DownloadService.startService(MyApplication.getInstance().getApplicationContext(), "http://117.158.178.202:8001/jlapk/jlfood.apk");
                Toast.makeText(BaseActivity.this, "已在后台下载更新", Toast.LENGTH_SHORT).show();
            }
        });
        localBuilder.setNegativeButton("取消", new OnClickListener() {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                BaseActivity.IS_CHECK_NO_CHANGE = true;
                paramAnonymousDialogInterface.dismiss();
            }
        });
        localBuilder.create().show();
    }

    public static void verifyStoragePermissions(Activity paramActivity) {
        try {
            if (ActivityCompat.checkSelfPermission(paramActivity, "android.permission.WRITE_EXTERNAL_STORAGE") != 0) {
                ActivityCompat.requestPermissions(paramActivity, PERMISSIONS_STORAGE, 1);
            }
            return;
        } catch (Exception e) {
        }
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_base);
        this.shouldReplaceFragment = getIntent().getIntExtra("shouldReplaceFragment", 0);
        try {
            this.versionName = getVersionName();
            this.rg = ((RadioGroup) findViewById(R.id.rg));
            this.rb1 = ((RadioButton) findViewById(R.id.rb1));
            this.rb2 = ((RadioButton) findViewById(R.id.rb2));
            this.rb3 = ((RadioButton) findViewById(R.id.rb3));
            this.rb4 = ((RadioButton) findViewById(R.id.rb4));
            this.listRadioButton = new ArrayList();
            this.listRadioButton.add(this.rb1);
            this.listRadioButton.add(this.rb2);
            this.listRadioButton.add(this.rb3);
            this.listRadioButton.add(this.rb4);
            this.fragment_FirstPages = new Fragment_FirstPages();
            this.fragment_classify = new ClassifityFragment();
            this.fragment_shoppingCat = new ShopCartFragment();
            this.fragment_user = new Fragment_User();
            this.fragmentList = new ArrayList();
            this.fragmentList.add(this.fragment_FirstPages);
            this.fragmentList.add(this.fragment_classify);
            this.fragmentList.add(this.fragment_shoppingCat);
            this.fragmentList.add(this.fragment_user);
            this.fragmentManager = getSupportFragmentManager();
            ((RadioButton) this.listRadioButton.get(this.shouldReplaceFragment)).setChecked(true);
            this.rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(RadioGroup paramAnonymousRadioGroup, int paramAnonymousInt) {

                    switch (paramAnonymousInt){
                        case R.id.rb1:
                            BaseActivity.this.replaceFragmentPage((Fragment) BaseActivity.this.fragmentList.get(0));

                            break;
                        case R.id.rb2:
                            BaseActivity.this.replaceFragmentPage((Fragment) BaseActivity.this.fragmentList.get(1));

                            break;
                        case R.id.rb3:
                            BaseActivity.this.replaceFragmentPage((Fragment) BaseActivity.this.fragmentList.get(2));

                            break;
                        case R.id.rb4:
                            BaseActivity.this.replaceFragmentPage((Fragment) BaseActivity.this.fragmentList.get(3));

                            break;
                            default:
                                break;
                    }

//                    int i = 0;
//                    if (i < BaseActivity.this.listRadioButton.size()) {
//                        BaseActivity.this.shouldReplaceFragment = i;
//                        if (paramAnonymousInt == ((RadioButton) BaseActivity.this.listRadioButton.get(i)).getId()) {
//                            if (paramAnonymousInt != R.id.rb3) {
//                            }
//                            if (BaseActivity.this.userId <= 0) {
//                            }
//                            BaseActivity.this.fragment_shoppingCat = new ShopCartFragment();
//                            BaseActivity.this.replaceFragmentPage(BaseActivity.this.fragment_shoppingCat);
//                        } else {
//                            i += 1;
//                            Toast.makeText(BaseActivity.this, "您还未登录，请先登录。", Toast.LENGTH_SHORT).show();
//                            BaseActivity.this.replaceFragmentPage((Fragment) BaseActivity.this.fragmentList.get(3));
//                            ((RadioButton) BaseActivity.this.listRadioButton.get(3)).setChecked(true);
//                            BaseActivity.this.replaceFragmentPage((Fragment) BaseActivity.this.fragmentList.get(i));
//                        }
//                    }
                }
            });
        } catch (Exception e) {

        }
//    replaceFragmentPage(this.fragment_FirstPages);
//    replaceFragmentPage(this.fragment_classify);
//    replaceFragmentPage(this.fragment_shoppingCat);
//    replaceFragmentPage(this.fragment_user);
    }

    protected void onResume() {
        super.onResume();
        this.userId = getSharedPreferences("JLAppData", 0).getInt("userId", 0);
        getNewVersionMessage();
    }

    public void replaceFragmentPage(Fragment paramFragment) {
        this.fragmentManager = getSupportFragmentManager();
        this.transaction = this.fragmentManager.beginTransaction();
        this.transaction.replace(R.id.framelayout, paramFragment);
        this.transaction.commit();
    }

    public class CheckVersionTask
            implements Runnable {
        Handler handler = new Handler() {
            public void handleMessage(Message paramAnonymousMessage) {
                super.handleMessage(paramAnonymousMessage);
                switch (paramAnonymousMessage.what) {

                    case 0:
                        CheckVersionTask.this.showUpdataDialog();
                        Log.d("aaaaaaaasetting", "对话框通知用户升级程序");
                        return;
                    case 1:
                        Toast.makeText(BaseActivity.this, "获取服务器更新信息失败", Toast.LENGTH_LONG).show();
                        CheckVersionTask.this.LoginMain();
                        return;
                    default:
                        Toast.makeText(BaseActivity.this, "下载新版本失败", Toast.LENGTH_LONG).show();
                        CheckVersionTask.this.LoginMain();
                        return;
                }

            }
        };

        public CheckVersionTask() {
        }

        private void LoginMain() {
            Intent localIntent = new Intent(BaseActivity.this, BaseActivity.class);
            BaseActivity.this.startActivity(localIntent);
            Tools.finishAll();
        }

        protected void downLoadApk() {
            final ProgressDialog localProgressDialog = new ProgressDialog(BaseActivity.this);
            localProgressDialog.setProgressStyle(1);
            localProgressDialog.setMessage("正在下载更新");
            localProgressDialog.setCancelable(false);
            localProgressDialog.show();
            new Thread() {
                public void run() {
                    try {
                        File localFile = BaseActivity.getFileFromServer("http://117.158.178.202:8001/jlapk/jlfood.apk", localProgressDialog);
                        CheckVersionTask.this.installApk(localFile);
                        localProgressDialog.dismiss();
                        return;
                    } catch (Exception localException) {
                        Message localMessage = new Message();
                        localMessage.what = 2;
                        CheckVersionTask.this.handler.sendMessage(localMessage);
                    }
                }
            }.start();
        }



        protected void installApk(File paramFile)
        {
            Intent localIntent = new Intent("android.intent.action.VIEW");
            if (Build.VERSION.SDK_INT >= 24)
            {
                Uri uri = FileProvider.getUriForFile(MyApplication.getInstance().getApplicationContext(), "com.jl.jlapp.fileprovider", paramFile);
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                localIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                localIntent.setDataAndType(uri, "application/vnd.android.package-archive");
            }
            else
            {
                startActivity(localIntent);
                localIntent.setDataAndType(Uri.fromFile(paramFile), "application/vnd.android.package-archive");
                localIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        }

        public void run() {
            try {
                Message localMessage1 = new Message();
                localMessage1.what = 0;
                this.handler.sendMessage(localMessage1);
                return;
            } catch (Exception localException) {
                Message localMessage2 = new Message();
                localMessage2.what = 1;
                this.handler.sendMessage(localMessage2);
            }
        }

        protected void showUpdataDialog() {
            Builder localBuilder = new Builder(BaseActivity.this);
            localBuilder.setTitle("版本升级");
            localBuilder.setMessage("检测到新版本，部分功能需升级后才可使用，请先升级！");
            localBuilder.setCancelable(false);
            localBuilder.setPositiveButton("确定", new OnClickListener() {
                public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                    Log.i("aaaaaaaasetting", "下载apk,更新");
                    paramAnonymousDialogInterface.dismiss();
                    CheckVersionTask.this.downLoadApk();
                }
            });
            localBuilder.create().show();
        }
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/activity/BaseActivity.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */